package com.m3839.sdk.common.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Description:NetworkUtils
 *
 * @author 陈宝阳
 * @create 2020/8/3 16:52
 */
public class NetworkUtils {

  /**
   * 判断是否有网络
   *
   * @param context
   * @return
   */
  public static boolean isConnected(Context context) {
    if(context == null || context.getApplicationContext() == null) {
      return true;
    }
    NetworkInfo info = getActiveNetworkInfo(context.getApplicationContext());
    return info != null && info.isConnected();
  }

  /**
   * 获取网络状态信息
   * @param context
   * @return
   */
  private static NetworkInfo getActiveNetworkInfo(Context context) {
    ConnectivityManager cm =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
      return null;
    }
    return cm.getActiveNetworkInfo();
  }
}
